package org.amphiprion.descentv2assistant.ui.views;

import org.amphiprion.descentv2assistant.R;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TabPageIndicator extends LinearLayout {
	private LinearLayout mTabLayout;
	private LayoutInflater mInflater;
	private ViewPager mViewPager;

	public TabPageIndicator(Context paramContext) {
		this(paramContext, null);
	}

	public TabPageIndicator(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		setHorizontalScrollBarEnabled(false);

		this.mInflater = LayoutInflater.from(paramContext);

		this.mTabLayout = this;

	}

	public void setCurrentItem(int paramInt) {
		// this.mSelectedTabIndex = paramInt;
		int nb = this.mTabLayout.getChildCount();
		for (int i = 0; i < nb; i++) {
			View localView = this.mTabLayout.getChildAt(i);
			localView.setSelected(i == paramInt);
		}
		mViewPager.setCurrentItem(paramInt);

	}

	public void setViewPager(ViewPager paramViewPager) {

		PagerAdapter localPagerAdapter = paramViewPager.getAdapter();
		this.mViewPager = paramViewPager;
		mTabLayout.removeAllViews();
		mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int paramInt) {
				setCurrentItem(paramInt);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		for (int i = 0; i < localPagerAdapter.getCount(); i++) {
			addTab(i);
		}
		setCurrentItem(0);
	}

	private void addTab(int paramInt) {
		TabView localTabView = (TabView) this.mInflater.inflate(R.layout.tab_item, null);
		localTabView.setFocusable(true);
		localTabView.init(this, ((TitleProvider) mViewPager.getAdapter()).getTitle(paramInt), paramInt);
		localTabView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TabView tv = (TabView) v;
				setCurrentItem(tv.mIndex);
			}
		});

		this.mTabLayout.addView(localTabView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, -1, 1.0F));
	}

	// ///////////////////////////////////
	// ///////////////////////////////////

	public static class TabView extends LinearLayout {
		private int mIndex;
		private TabPageIndicator mParent;

		public TabView(Context paramContext, AttributeSet paramAttributeSet) {
			super(paramContext, paramAttributeSet);
		}

		public int getIndex() {
			return this.mIndex;
		}

		public void init(TabPageIndicator paramTabPageIndicator, String paramString, int paramInt) {
			this.mParent = paramTabPageIndicator;
			this.mIndex = paramInt;
			((TextView) findViewById(R.id.txtTab)).setText(paramString);
		}

		// public void onMeasure(int paramInt1, int paramInt2)
		// {
		// super.onMeasure(paramInt1, paramInt2);
		// if ((this.mParent.mMaxTabWidth > 0) && (getMeasuredWidth() >
		// this.mParent.mMaxTabWidth))
		// super.onMeasure(View.MeasureSpec.makeMeasureSpec(this.mParent.mMaxTabWidth,
		// 1073741824), paramInt2);
		// }
	}
}
